package com.ibiz.webapp.controller;

import javax.validation.constraints.NotBlank;

import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.ibiz.ibizutil.security.AuthenticationUser;
import com.ibiz.workflow.WFServiceImpl;

@RestController
@RequestMapping("/webapp/bx/worklist")
public class WorklistDBController {

	@Autowired
	WFServiceImpl wfServiceImpl;

	@GetMapping(value = "/redirectview/getmodel")
	public ResponseEntity<JSONObject> wfdesign(@Validated @NotBlank(message = "srfkey不允许为空") @RequestParam("srfkey") String srfkey) {
		JSONObject ret = new JSONObject();
	
		
		Task task = wfServiceImpl.findTask(AuthenticationUser.getAuthenticationUser().getUserid(),srfkey) ;
		if(task==null) {
			ret.put("ret", "903") ;
			ret.put("errorInfo", "查询待办错误，当前用户无次待办操作。") ;
			return ResponseEntity.ok().body(ret);
		}
		ProcessInstance instance = wfServiceImpl.findInstance(task.getProcessInstanceId()) ;
		
		JSONObject params = new JSONObject();
		params.put("srfkey", instance.getBusinessKey()) ;
		
		ret.put("viewparams", params);
		ret.put("url", "");
		
		if("BX".equals(instance.getProcessDefinitionKey())) {
			ret.put("viewmodule", "Template");
			ret.put("viewtag", "BXDWFEditView3");
			ret.put("viewname", "BXDWFEditView3");
			ret.put("title", "报销单工作流视图");
			ret.put("openmode", "");
			ret.put("width", 750);
			ret.put("height", "0");
		}else {
			
		}
		
		return ResponseEntity.ok().body(ret);
	}
}